#include "Schedule.h"
#include "SC_Init.h"	// MCU initialization header file, including all firmware library header files
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
#include "sc95f_wdt.h"
//#include "display.h"
#include "machine_stat.h"
#include "lcd_comm.h"
//#include "r_cg_wdt.h"
//#include "AiP33626"
//#include "display.h"
#include "comm.h"
#include "comm_wifi.h"
#include "key_process.h"
#include "buzzer.h"
#include "time_8563.h"
#include "flash.h"
#include "open_power_code.h"
uint8_t TimeBaseCounter;
uint8_t OldTimeBaseCounter;
uint8_t blink_flag_100ms=0;
uint8_t blink_flag_250ms=0;
uint8_t blink_flag_500ms=0;
uint8_t blink_flag_1000ms=0;
uint8_t blink_flag_2000ms=0;
uint8_t commfail_cnt=0;
uint8_t Commfail_Flag=0;
uint8_t cmd_times=0;
uint16_t err_time_cnt=0;
uint8_t temp_times=0;
//extern uint8_t stat_i;
#define			TIMER1_MATCH				0
#define			TIMER2_MATCH				1
#define			TIMER3_MATCH				2
#define			TIMER4_MATCH				3
#define			TIMER5_MATCH				4
#define			TIMER6_MATCH				5
#define			TIMER7_MATCH				6
#define			TIMER8_MATCH				7
#define			TIMER9_MATCH				8
uint8_t code tTimeBaseTable[256]=
{
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER9_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER6_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER7_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER6_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER8_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER6_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER7_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER6_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER5_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER4_MATCH   ,	TIMER1_MATCH   ,	TIMER2_MATCH   ,	TIMER1_MATCH   ,	TIMER3_MATCH   ,
};

void ScheduleWorkOption1(void);
void ScheduleWorkOption2(void);
void ScheduleWorkOption3(void);
void ScheduleWorkOption4(void);
void ScheduleWorkOption5(void);
void ScheduleWorkOption6(void);
void ScheduleWorkOption7(void);
void ScheduleWorkOption8(void);
void ScheduleWorkOption9(void);


void ScheduleWorkOption1(void)//5mS*2     每10ms一次
{
	
	KeyProcess();//按键计数
	machine_stat();
	WIFI_Task();
	//wifi_stat.wifi_cmd=wifi_comm_process(wifi_stat.wifi_cmd);//WiFi通信处理
    wifi_display();
	comm_process();//主机板通信
    ParamTask();//mcu中写flash
	BuzzerTask();
	WDT_SetReload();
	display_send();//显示
	// display_process();
}
void ScheduleWorkOption2(void)//5mS*4     每20ms一次
{
	
	//GPIO_TogglePin(GPIO0,GPIO_PIN_0);

	display_send();
	
}
void ScheduleWorkOption3(void)//5mS*8     40
{
	
	display_send();
}
void ScheduleWorkOption4(void)//5mS*16    80     0.08
{
	time_process();//时间处理 iic通信
	display_send();
}
void ScheduleWorkOption5(void)//5mS*32     160
{
	WDT_SetReload();
	display_send();
    
}

void ScheduleWorkOption6(void)//5mS*64    320
{			
	display_send();
}
void ScheduleWorkOption7(void)//5mS*128   640
{
	display_send();
}
void ScheduleWorkOption8(void)//5mS*256  1280
{
	display_send();
}
void ScheduleWorkOption9(void)//5mS*256   1280
{
	display_send();
}

void (*ScheduleWorkOption[9])(void) =
{
	ScheduleWorkOption1,//2
	ScheduleWorkOption2,//4
	ScheduleWorkOption3,//8
	ScheduleWorkOption4,//16
	ScheduleWorkOption5,//32
	ScheduleWorkOption6,//64
	ScheduleWorkOption7,//128
	ScheduleWorkOption8,//256
	ScheduleWorkOption9,//256
};

void Schedule_init(void)
{
	machine_init();
	key_process_init();
	comm_rinnai_init();
	dispaly_init();
	// AiP650_display(0,0,0,0);
    ParamTask_Init();
	Buzzerinit(); 
	WIFI_Init();
}
void Schedule_flag(void)//5ms//定时器中断运行内容
{
	static uint16_t times_flag=0;
	TBaseCount();
	times_flag++;
	err_time_cnt++;
	if((times_flag%20)==0)//100
	{
		blink_flag_100ms=~blink_flag_100ms;///0   ff
		
	}
	if((times_flag%100)==0)
	{
		blink_flag_500ms=~blink_flag_500ms;// 0   ff
	}
	if((times_flag%50)==0)
	{
		blink_flag_250ms=~blink_flag_250ms;
	}
	if((times_flag%200)==0)
	{
		blink_flag_1000ms=~blink_flag_1000ms;
		times_flag=0;
		if(IsFactoryMode() == 0){
			commfail_cnt++;
			if(commfail_cnt>=3){
				commfail_cnt=0;
				Commfail_Flag=1;
			}
		}

	}
}